package com.quanyan.comment.web;

import com.quanyan.api.APIResponse;
import com.quanyan.comment.reqeust.ReqBaseParam;
import com.quanyan.comment.reqeust.ReqCommentParam;
import com.quanyan.comment.reqeust.ReqCommentQuery;
import com.quanyan.comment.service.CommentService;
import com.quanyan.comment.service.CommentSumService;
import com.quanyan.comment.service.CommentTagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created by Administrator on 2016/10/19.
 */
@RestController
@RequestMapping("/api/comment/comment/")
public class CommentController {

    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);


    @Autowired
    private CommentService commentService;

    @Autowired
    private CommentTagService commentTagService;

    @Autowired
    private CommentSumService commentSumService;

    @RequestMapping(value = "monitor",method = RequestMethod.POST)
    public APIResponse monitor(){
        return APIResponse.returnSuccess();
    }


    @RequestMapping(value = "query",method = RequestMethod.POST)
    public APIResponse commentQuery(@RequestBody ReqCommentQuery reqCommentQuery){
        return APIResponse.returnSuccess(commentService.queryCommentTwo(reqCommentQuery));
    }


    @RequestMapping(value = "add",method = RequestMethod.POST)
    public APIResponse commentQuery(@RequestBody ReqCommentParam reqCommentParam){
        commentService.commentAdd(reqCommentParam);
        return APIResponse.returnSuccess();
    }



    @RequestMapping(value = "tag",method = RequestMethod.POST)
    public APIResponse commentTagQuery(@RequestBody ReqCommentQuery reqCommentQuery){
        return commentTagService.commentTagQuery(reqCommentQuery);
    }



    @RequestMapping(value = "avg",method = RequestMethod.POST)
    public APIResponse commentAvg(@RequestBody ReqBaseParam reqBaseParam){

        try {
            return APIResponse.returnSuccess(commentSumService.getAvgByBusinessId(reqBaseParam));
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常");
        }
    }


    @RequestMapping(value = "groupTagQuery",method = RequestMethod.POST)
    public APIResponse tagGroup(@RequestBody Map map){
        try {
            Object obj = map.get("category_id");
            if (obj == null){
                return APIResponse.returnFail("参数不能为空！");
            }
            return APIResponse.returnSuccess(commentTagService.queryGroupLabel(Byte.parseByte(obj.toString())));
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常");
        }
    }

    @RequestMapping(value = "tagCategory",method = RequestMethod.POST)
    public APIResponse tagDaren(@RequestBody Map map){
        try {
            Object obj = map.get("category_id");
            if (obj == null){
                return APIResponse.returnFail("参数不能为空！");
            }
            return APIResponse.returnSuccess(commentTagService.queryDarenTag(Byte.parseByte(obj.toString())));
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常");
        }
    }

}
